﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAOTicketReservationSystem;

namespace ManageEvents
{
    public partial class FormCategories : Form
    {
        private Dao dao;

        public FormCategories()
        {
            InitializeComponent();
        }

        private void FormEditCategories_Load(object sender, EventArgs e)
        {
            this.dao = new Dao();
            categoryBindingSource.DataSource = this.dao.GetAllCategories();
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }       

        private void buttonAdd_Click(object sender, EventArgs e)
        {            
            //FormAddEdit formAddEdit = new FormAddEdit();
            //formAddEdit.IsCategory = true;
            //formAddEdit.ForAdd = true;
            //formAddEdit.LabelText = "Enter cateogory name:";
            //formAddEdit.ButtonText = "Add"; 
            //if (formAddEdit.ShowDialog() == DialogResult.OK)
            //{
            //    categoryBindingSource.DataSource = Dao.GetAllCategories();
            //}
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.dao.Save();
            categoryBindingSource.DataSource = this.dao.GetAllCategories();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.dao.Cancel();
            categoryBindingSource.DataSource = this.dao.GetAllCategories();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewCategories.SelectedRows.Count > 0 && ((Category)dataGridViewCategories.SelectedRows[0].DataBoundItem) != null)
            {
                if (MessageBox.Show("You are going to delete selected category! Are you sure?", "Confirm delete",
                    MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Category currentCategory = (Category)dataGridViewCategories.SelectedRows[0].DataBoundItem;
                    string deleteCategoryResult = dao.DeleteCategoryById(currentCategory.CategoryID);
                    if (deleteCategoryResult == Dao.GetSuccessMessage())
                    {
                        MessageBox.Show("Category deleted!", "Success");
                        categoryBindingSource.DataSource = dao.GetAllCategories();
                    }
                    else
                    {
                        MessageBox.Show(deleteCategoryResult, "Error");
                    }
                    buttonCancel_Click(null, EventArgs.Empty);
                }
            }
            else 
            {
                MessageBox.Show("Select category to delete!");
            }
        }
    }
}
